#include "TcpServer.hpp"
#include <memory>

void Usage(const string &pro)
{
    cout << "Usage:\n\t" << pro << " port" << endl;
}

Response task(const Request &req)
{
    Response res(0, 0);
    int x = req._x;
    int y = req._y;
    switch (req._op)
    {
    case '+':
        res._result = x + y;
        break;
    case '-':
        res._result = x - y;
        break;
    case '*':
        res._result = x * y;
        break;
    case '/':
        if(y == 0)
        {
            res._code = 1;
            log_message(Warning, "/ zero error");
        }
        else
        {
            res._result = x / y;
        }
        break;
    case '%':
        if(y == 0)
        {
            res._code = 2;
            log_message(Warning, "%% zero error");
        }
        else
        {
            res._result = x % y;
        }
        break;
    default:
        log_message(Warning, "op error");
        break;
    }
    return res;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(0);
    }

    unique_ptr<TcpServer> up_ts(new TcpServer(task, stoi(argv[1])));
    up_ts->Init();
    up_ts->Start();
    return 0;
}